//
//  ImageViewWithGesture.m
//  UI04_事件_触摸_手势
//
//  Created by wyx on 16/3/18.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ImageViewWithGesture.h"

@implementation ImageViewWithGesture

-(instancetype)initWithImage:(UIImage *)image{
    self = [super initWithImage:image];
    if (self) {
        //开启用户交互
        self.userInteractionEnabled = YES;
        //调用方法
        [self tapGesture];
        [self pinchGesture];
        [self swipeGesture];
        [self longPressGesture];
        [self rotationGeesture];
        [self panGesture];
    }
    return self;
}
#pragma mark - 轻拍手势
- (void)tapGesture{
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletap:)];
    //手势加在对象本身上
    [self addGestureRecognizer:tap1];
    [tap1 release];
    //设置轻拍次数
    tap1.numberOfTapsRequired = 2;
}
- (void)handletap:(UITapGestureRecognizer *)tap{
    //imageview的 transform 恢复初始状态(动画效果)
    [UIView animateWithDuration:0.5 animations:^{
        
        self.transform = CGAffineTransformIdentity;
    }];
}
#pragma mark - 长按手势
- (void)longPressGesture{
    UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [self addGestureRecognizer:longGes];
    [longGes release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
    }
}
#pragma mark - 轻扫手势
- (void)swipeGesture{
    UISwipeGestureRecognizer *swipsges = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    //轻扫方法
    swipsges.direction = UISwipeGestureRecognizerDirectionLeft;
}
-(void)swip:(UISwipeGestureRecognizer *)swip{
    
}
#pragma mark - 捏合
- (void)pinchGesture{
    UIPanGestureRecognizer *pinges = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlepinch:)];
    [self addGestureRecognizer:pinges];
    [pinges release];
}
-(void)handlepinch:(UIPinchGestureRecognizer *)handlepinch{
    //修改 ImageView 对象的 transform 属性
    self.transform = CGAffineTransformScale(self.transform, handlepinch.scale, handlepinch.scale);
    handlepinch.scale = 1;
}
#pragma mark - 旋转
- (void)rotationGeesture{
    UIRotationGestureRecognizer *rote = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationHandle:)];
    [self addGestureRecognizer:rote];
    [rote release];
}
- (void)rotationHandle:(UIRotationGestureRecognizer *)rotation{
    //修改 imageView 的本身属性
    self.transform = CGAffineTransformRotate(self.transform, rotation.rotation);
    rotation.rotation = 0;
}
#pragma mark - 位移
- (void)panGesture{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panhandle:)];
    [self addGestureRecognizer:pan];
    [pan release];
    
}
-(void)panhandle:(UIPanGestureRecognizer *)pan{
    self.transform = CGAffineTransformTranslate(self.transform, [pan translationInView:self].x, [pan translationInView:self].y);
    //复位
    [pan setTranslation:CGPointZero inView:self];
}
@end
